<%@ page import="java.sql.*" %>
<% Class.forName("com.mysql.jdbc.Driver");

String sql, url;
Connection conn;
ResultSet rs;
Statement stmt;
PreparedStatement ps;

String id = request.getParameter("id").trim();

try 
{
	Class.forName("com.mysql.jdbc.Driver");
	url = "jdbc:mysql://localhost/student_db";
	conn = DriverManager.getConnection(url, "root", "");
	
	stmt = conn.createStatement();
	rs = stmt.executeQuery("Select * FROM student WHERE id =" + id);
	
	while(rs.next())
	{
		
	out.print("<form action=\"");
	out.print( request.getRequestURI() );
	out.print("\" method=\"post\">");
	out.print("<input type=\"hidden\" name=\"id\" value=\"" + rs.getObject(1).toString() + "\"><br>");
	out.print("Name :");
	out.print("<input type=\"text\" name=\"name\" value=\"" + rs.getObject(2).toString() + "\"><br>");
	out.print("Address :");
	out.print("<input type=\"text\" name=\"address\" value=\"" + rs.getObject(3).toString() + "\"><br>");
	out.print("Telephone :");
	out.print("<input type=\"text\" name=\"telephone\" value=\"" + rs.getObject(4).toString() + "\"><br>");
	out.print("<br><br><input type=\"submit\" name=\"insert\" value=\"Update Student\">");
	out.print("<input type=\"button\" onClick=\"parent.location='Main'\" value=\"Cancel\">");
	
	}
} 
catch (ClassNotFoundException cnfe) 
{
		out.println("" + cnfe);
}
catch (SQLException sqle)
{
		out.println("" + sqle);
}

if(request.getParameter("insert") != null)
{
	String name = request.getParameter("name").trim();
	String address = request.getParameter("address").trim();
	String telephone = request.getParameter("telephone").trim();
	boolean proceed = false;
	
	if(name != null && address != null && telephone != null)
		if(name.length() > 0 && address.length() > 0 && telephone != null)
			proceed = true;
	
	try 
	{
		Class.forName("com.mysql.jdbc.Driver");
		url = "jdbc:mysql://localhost/student_db";
		conn = DriverManager.getConnection(url, "root", "");
	
		sql = ("UPDATE student SET name=?, address=?, phone=? WHERE id =" + id);
		ps = conn.prepareStatement(sql);
		
		if(proceed)
		{
			ps.setString(1, name);
			ps.setString(2, address);
			ps.setString(3, telephone);
			ps.executeUpdate();
		}
		response.sendRedirect("http://localhost:8080/Student/Main.jsp");
	
	} 
	catch (ClassNotFoundException cnfe) 
	{
			out.println("" + cnfe);
	}
	catch (SQLException sqle)
	{
			out.println("" + sqle);
	}
}

%>